// led
#if 0
#include "led.h"

int main(void)
{
	int i;
	led_init();

	while (1) {
		led_on(0);
		for (i = 0; i < 5000000; i++);
		led_off(0);
		
		led_on(1);
		for (i = 0; i < 5000000; i++);
		led_off(1);
		
		led_on(2);
		for (i = 0; i < 5000000; i++);
		led_off(2);
	}
}

#endif



// led2
#if 0
#include "led.h"
#include "misc.h"  // 包含 SysTick 相关的定义
static int flag;

void mysystick_handler(void)
{
	flag--;
}

void delay_ms(int nms)
{
	flag = nms;
	
	while (flag);
}

int main(void)
{
	// int i;
	led_register_init();

	/*
		sYSTICK定时时间到达会产生系统异常
		异常会导致arm核暂停正在执行的程序，将函数执行的地址切换到异常对应的处理函数
		sYsTICK对应的异常处理函数，ARM核已经规划好了
		在startup_stm32f10x.s
			sysTick_Handler
	*/
	SysTick_Config(9000);									// 设置计数值定时时长 9000 / 9000000 == 1ms
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);	// 初始化sysTicK频率 9MHz == 9000000Hz

	while (1) {
		led_register_on(0);
		// for (i = 0; i < 5000000; i++);
		delay_ms(1000);
		led_register_off(0);
		
		led_register_on(1);
		// for (i = 0; i < 5000000; i++);
		delay_ms(1000);
		led_register_off(1);
		
		led_register_on(2);
		// for (i = 0; i < 5000000; i++);
		delay_ms(1000);
		led_register_off(2);
	}
}

#endif



// key
#if 0
#include "key.h"
#include "led.h"

int main(void)
{
	int8_t ret;
	
	key_init();
	led_init();
	
	while (1) {
		ret = key_status(0);
		if (ret)
			led_on(0);
		
		ret = key_status(1);
		if (ret)
			led_off(0);
	}
}

#endif


// beep
#if 0
#include "key.h"
#include "beep.h"

int main(void)
{
	int8_t ret;
	
	key_init();
	beep_init();
	
	while (1) {
		ret = key_status(0);
		if (ret)
			beep_open();
		
		ret = key_status(1);
		if (ret)
			beep_close();
	}
}

#endif




// remote
#if 0
#include "led.h"
#include "remote.h"

int main(void) 
{
	int i;
    // 初始化
    remote_init();
    led_init();

    while (1) {
        if (remote_received()) {  // 检查是否接收到红外信号
            led_on(1);
			for (i = 0; i < 1000000; i++);
			led_off(1);
        } 
    }
}

#endif

// iwdg
#if 0
#include "iwdg.h"
#include "led.h"
#include "key.h"
#include "beep.h"
#include "delay.h"
int main(void) 
{
	int flag = 1;
	led_init();
	key_init();
	iwdg_init(5);
	beep_init();
	delay_init();

	beep_open();
	delay_ms(1000);
	beep_close();
	
	
	while (1) {
		if (key_status(0)) // 按下按键，模拟程序崩溃
			flag = 0;
		
		led_on(0);
		delay_ms(1000);
		led_off(0);
		
		led_on(1);
		delay_ms(1000);
		led_off(1);
		
		led_on(2);
		delay_ms(1000);
		led_off(2);
		
		if (flag)
			iwdg_feed(); // 喂狗操作
	}
}

#endif

// exti
#if 0
#include "iwdg.h"
#include "led.h"
#include "key.h"
#include "beep.h"
#include "delay.h"
#include "eint_key.h"

static int flag = 1;

static void my_key1_handler(void)
{
	flag = 0; 			// 按下按键，模拟程序崩溃
}

static void my_key2_handler(void)
{
	// ...
}

static void my_key3_handler(void)
{
	// ...
}


int main(void) 
{
	led_init();
	eint_key_init();	// 初始化按键中断
	iwdg_init(5);
	beep_init();
	delay_init();

	/* 注册中断处理函数 */
	set_key_handlers(my_key1_handler, my_key2_handler, my_key3_handler);
	
	beep_open();
	delay_ms(1000);
	beep_close();
	
	
	while (1) {
		led_on(0);
		delay_ms(1000);
		led_off(0);
		
		led_on(1);
		delay_ms(1000);
		led_off(1);
		
		led_on(2);
		delay_ms(1000);
		led_off(2);
		
		if (flag)
			iwdg_feed(); // 喂狗操作
	}
}

#endif

// usart1
#if 0
#include "key.h"
#include "led.h"
#include "delay.h"
#include "eint_key.h"
#include "usart1.h"

static void my_key1_handler(void)
{
	usart1_send_byte(3);
}

static void my_key2_handler(void)
{
}

static void my_key3_handler(void)
{
}

int main(void) 
{
	eint_key_init();	// 初始化按键中断
	usart1_init();
	led_init();
	delay_init();

	/* 注册中断处理函数 */
	set_key_handlers(my_key1_handler, my_key2_handler, my_key3_handler);
	
	while (1) {
		delay_ms(500);
		if (usart1_recv_byte()) {
			led_on(0);
			delay_ms(200);
			led_off(0);
		}
	}
}

#endif

// usart1_exti
#if 0
#include "key.h"
#include "led.h"
#include "delay.h"
#include "eint_key.h"
#include "usart1.h"
#include "usart1_exti.h"
#include <string.h>

static void my_key1_handler(void)
{
	usart1_send_byte('a');
}

static void my_key2_handler(void)
{
	char str[] = "hello";
	usart1_send_str(str);
}

static void my_key3_handler(void)
{
	student st = {1, 18, "Tom"};
	
	usart1_send_struct(&st);
}

static void my_handler(void) 
{
	char str[10];
	
	/* 接收并判断亦可使用有限状态机实现 */
	usart1_recv_str(str, 10);
	if (strstr(str, "on") != NULL) {
		led_on(1);
	} else if (strstr(str, "off") != NULL) {
		led_off(1);
	}
}

int main(void) 
{

	eint_key_init();	// 初始化按键中断
	usart1_init();
	usart1_exti_init();
	led_init();
	delay_init();

	/* 注册中断处理函数 */
	set_key_handlers(my_key1_handler, my_key2_handler, my_key3_handler);
	set_handlers(my_handler);
	
	while (1) {
		delay_ms(500);
	}
}

#endif

// dht11
#if 0
#include "key.h"
#include "led.h"
#include "delay.h"
#include "eint_key.h"
#include "usart1.h"
#include "usart1_exti.h"
#include <string.h>
#include <stdio.h>
#include "dht11.h"


static void my_key1_handler(void)
{
	
	uint8_t temp = 0, humi = 0;
	char data[100];
	
	// 向上位机发数据
	dht11_read_data(&temp, &humi);
	snprintf(data, sizeof(data), "Temp: %u, Humi: %u", temp, humi);
	
	usart1_send_str(data);
}

static void my_key2_handler(void)
{
}

static void my_key3_handler(void)
{
}

int main(void) 
{
	dht11_init();
	led_init();
	key_init();
	eint_key_init();	// 初始化按键中断
	usart1_init();
	usart1_exti_init();
	delay_init();

	/* 注册中断处理函数 */
	set_key_handlers(my_key1_handler, my_key2_handler, my_key3_handler);

	
	while (1) {
		delay_ms(1000);
	}
}

#endif

// sht30
#if 0
#include "key.h"
#include "led.h"
#include "delay.h"
#include "eint_key.h"
#include "usart1.h"
#include "usart1_exti.h"
#include <string.h>
#include <stdio.h>
#include "sht30.h"


static void my_key1_handler(void)
{
	double temp = 0, humi = 0;
	char data[100];
	
	// 向上位机发数据
	sht30_read_data(&temp, &humi);
	snprintf(data, sizeof(data), "Temp: %f, Humi: %f", temp, humi);
	
	usart1_send_str(data);
}

static void my_key2_handler(void)
{
}

static void my_key3_handler(void)
{
}

int main(void) 
{
	sht30_init();
	led_init();
	key_init();
	eint_key_init();	// 初始化按键中断
	usart1_init();
	usart1_exti_init();
	delay_init();

	/* 注册中断处理函数 */
	set_key_handlers(my_key1_handler, my_key2_handler, my_key3_handler);

	set_repeat_mode();
	
	while (1) {
		delay_ms(1000);
	}
}

#endif

// oled
#if 0
#include "key.h"
#include "led.h"
#include "delay.h"
#include "eint_key.h"
#include "usart1.h"
#include "usart1_exti.h"
#include <string.h>
#include <stdio.h>
#include "sht30.h"
#include "oled.h"

static void my_key1_handler(void)
{
	double temp = 0, humi = 0;
	char data[100];
	
	// 向上位机发数据
	sht30_read_data(&temp, &humi);
	snprintf(data, sizeof(data), "Temp: %.2f, Humi: %.2f%%", temp, humi);
	
	usart1_send_str(data);
	
	OLED_Clear();
	OLED_ShowString(0, 0, (u8 *)data, 16);
}

static void my_key2_handler(void)
{
}

static void my_key3_handler(void)
{
}

int main(void) 
{
	sht30_init();
	led_init();
	key_init();
	eint_key_init();	// 初始化按键中断
	usart1_init();
	usart1_exti_init();
	delay_init();
	OLED_Init();
	OLED_Display_On();

	/* 注册中断处理函数 */
	set_key_handlers(my_key1_handler, my_key2_handler, my_key3_handler);

	set_repeat_mode();
	
	while (1) {
		delay_ms(1000);
	}
}

#endif

// rtc
#if 0
#include "key.h"
#include "led.h"
#include "delay.h"
#include "eint_key.h"
#include "usart1.h"
#include "usart1_exti.h"
#include <string.h>
#include <stdio.h>
#include "sht30.h"
#include "oled.h"
#include "rtc.h"


int main(void) 
{
	char data[100];
	struct time_st my_time = {2025, 2, 21, 5, 17, 4, 15};

	delay_init();
	OLED_Init();
	OLED_Display_On();
	rtc_init(&my_time);
	
	while (1) {
		get_time(&my_time);
		snprintf(data, sizeof(data), "%d/%d/%d %d:%d:%d     ", \
		my_time.year, my_time.month, my_time.day, my_time.hour, my_time.minute, my_time.second);
	
		//OLED_Clear();
		OLED_ShowString(0, 0, (u8 *)data, 16);
		delay_ms(1000);
	}
}

#endif

// Nokia
#if 0
#include "key.h"
#include "led.h"
#include "delay.h"
#include "eint_key.h"
#include "usart1.h"
#include "usart1_exti.h"
#include <string.h>
#include <stdio.h>
#include "sht30.h"
#include "oled.h"
#include "rtc.h"


int main(void) 
{
	int i;

	delay_init();
	OLED_Init();
	OLED_Display_On();
	
	while (1) {
		for (i = 0; i < 12; i++) {
			pic_nokia(i);
			delay_ms(70);
		}
		delay_ms(3000);
	}
}

#endif

// ldt
#if 0
#include "delay.h"
#include "ldt.h"

int main() 
{
    uint16_t num = 0;           

    ldt_init();                    
    delay_init();

    while (1) {
        ldt_show_number(num++, 1000);  
		num %= 10000;
    }
}

#endif

// adc
#if 0
#include "delay.h"
#include "ldt.h"
#include "adc_res.h"

int main() 
{
    uint16_t num = 0;           

    ldt_init();                    
    delay_init();
	adc_init();

    while (1) {
		num = get_adc_value();
        ldt_show_number(num, 1000);  
    }
}

#endif

// esp8266
#if 0
#include "led.h"
#include "beep.h"
#include "delay.h"
#include "sht30.h"
#include "esp8266.h"
#include "stdio.h"
#include "string.h"

#define BUFSIZE	128

int main(void)
{
	double temp, humi;
	char buf1[BUFSIZE] = {0};
	char buf2[BUFSIZE] = {0};
	
	/* 使用sht30采集温湿度通过wifi发送到上位机 */
	sht30_init();
	esp8266_init();
	delay_init();
	led_init();
	
	led_on(0);
	esp8266_link_wifi("OnePlus", "m623eumy");
	delay_ms(500);
	led_off(0);
	
	esp8266_connect("192.168.47.119", "1234");
	delay_ms(500);
	
	set_repeat_mode();
	
	while (1) {
		sht30_read_data(&temp, &humi);
		snprintf(buf1, BUFSIZE, "温度:%.2f, 湿度:%.2f", temp, humi);
		snprintf(buf2, BUFSIZE, "%d", strlen(buf1));
		led_on(2);
		esp8266_send(buf1, buf2);
		esp8266_send("hello", "5");
		led_off(2);
		delay_ms(1000);
	}
}

#endif

// iot_uplode
#if 1
#include "led.h"
#include "buzzer.h"
#include "delay.h"
#include "esp8266.h"
#include "iot.h"
#include "string.h"
#include "sht30.h"

#define SIZE 2
#define API_KEY "eyJpYXQiOjE3NDA0NjYxODYsImV4cCI6MjA1NTgyNjE4NiwiYWxnIjoiSFMyNTYifQ.eyJpZCI6NDkzNn0.feXzuol4jiPgvtnkWc_iJIKwqleZnOOVqWXi4BdqUYc:"
//API_KEY需要修改成自己账号(保留冒号)

int main(void)
{
	int sensor_id_arrary[SIZE] = {14076, 14171};//需要修改成自己的传感器的ID号
	double sensor_data_arrary[SIZE] = {0};// 数据
	double temp = 0, humi = 0;

	led_init();
	buzzer_init();
	delay_init();
	esp8266_init();
	sht30_init();
	
	led_on(0);
	esp8266_link_wifi("OnePlus", "m623eumy");//需要修改成自己的手机热点
	delay_ms(500);
	led_off(0);
	set_repeat_mode();
	
	while(1)
	{
		// 向上位机发数据
		sht30_read_data(&temp, &humi);
		sensor_data_arrary[0] = (double)temp;
		sensor_data_arrary[1] = (double)humi;
		upload_device_datas("119.29.98.16", "5062", API_KEY, sensor_id_arrary, sensor_data_arrary, SIZE);
		//IP地址不需要修改
		led_on(2);
		delay_ms(2000);
		led_off(2);
		delay_ms(1000);
	}
}

#endif
